package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
			System.out.println("Please specify parameters: <amount of test data> <file> <format>");
			return;
			}
		
			int amount = Integer.parseInt(args[0]);
			File file = new File(args[1]);
			String format = args[2];
			
			if (file.exists()){
				System.out.println("File exists, please remove it manually: " + file);
			}
			
			List<ContactData> contacts = generateRandomContacts(amount);
			if ("csv".equals(format)){
				saveContactsToCsvFile(contacts, file);	
			} else if ("xml".equals(format)){
				saveContactsToXmlFile(contacts, file);
			} else {
				System.out.println("Unknown format: " + format);
				return;
			}
	}
	
	public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
		List<ContactData> list = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null){
			String[] part = line.split(",");
			ContactData contact = new ContactData()
				.withFirstName(part[0])
				.withLastName(part[1])
				.withEmail(part[2])
				.withAddress(part[3])
				.withPhoneHome(part[4])
				.withPhoneMobile(part[5])
				.withDayOfBirth(part[6])
				.withMonthOfBirth(part[7])
				.withYearOfBirth(part[8]);
			list.add(contact);
			line = bufferedReader.readLine();
		}
		bufferedReader.close();
		reader.close();
		
		return list;
	}
	
	public static List<ContactData> loadContactsFromXmlFile(File file) {
		XStream xstream = new XStream(); 
		xstream.alias("contact", ContactData.class);
	    return (List<ContactData>) xstream.fromXML(file);
}


	private static void saveContactsToXmlFile(List<ContactData> contacts,File file) throws IOException {
		XStream xstream = new XStream(); 
		xstream.alias("contact", ContactData.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
		
	}

	private static void saveContactsToCsvFile(List<ContactData> contacts, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for(ContactData contact : contacts){
			writer.write(contact.getFirstName() + "," + contact.getLastName() + "," + contact.getEmail() + "," + contact.getAddress() + "," + contact.getPhoneHome() + "," + 
		contact.getPhoneMobile() + "," + contact.getDayOfBirth() + "," + contact.getMonthOfBirth() + "," + contact.getYearOfBirth() +",!" +"\n");
		}
		writer.close();
		
	}
	
	public static List<ContactData> generateRandomContacts(int amount) {
		List<ContactData> list = new ArrayList<ContactData>();
		for(int i=0; i<amount; i++)
		{
			ContactData contact = new ContactData()
				.withFirstName(generateRandomFisrtName())
				.withLastName(generateRandomLastName())
				.withAddress(generateRandomString())
				.withPhoneHome(generateRandomPhone())
				.withPhoneMobile(generateRandomPhone())
				.withPhoneWork(generateRandomPhone())
				.withEmail(generateRandomEmail())
				.withDayOfBirth(generateRandomDayOfBirth())
				.withMonthOfBirth(generateRandomMonth())
				.withYearOfBirth(generateRandomYear());
			list.add(contact);
		}
		return  list;
	}
	
	  public static String generateRandomFisrtName() {
		  Random rnd = new Random();
		  String[] arrayOfNames = { "Tanya", "Nadya", "Irina", "Olga",  
	            "Kira", "Oksana", "Natalya", "Katya", "Vita", "Vika",  
	            "Rosa", "Lesya", "Alina", "Marina",  
	            "Sasha", "Kristina", "Sveta", "Nastya" };
		  return arrayOfNames[rnd.nextInt(arrayOfNames.length)];
			
	  }  
	  
	public static String generateRandomLastName() {
		  Random rnd = new Random();
		  String[] arrayOfLastNames = {"Orlova", "Kramarchyk", "Lorionova", "Melnik",  
	            "Kisel", "Koval", "Sivnitskaya", "Vodonaeva", "Alekseeva", "Kravchyk",  
	            "Momot", "Zutneva", "Baranseva", "Avdeeva",  
	            "Viter", "Golovchyk", "Ivanova", "Sidorova", "Polischyk", "Orel", "Trachyk", "Golovatyuk", "Andrijchuk", "Korol"};
		  return arrayOfLastNames[rnd.nextInt(arrayOfLastNames.length)];
	   }

	
	public static String generateRandomString() {
		  Random rnd = new Random();
		  if (rnd.nextInt(3) == 0) {
				return "";
			} else {
				return "test" + rnd.nextInt();
			}
	  } 
    public static String generateRandomEmail() {
 	   Random rnd = new Random();  
 	   return "test" + rnd.nextInt(100) +"@mail.ru";
	   }

    public static String generateRandomPhone() {
 	   Random rnd = new Random();  
 	   int phone = 100000 + rnd.nextInt(100000); 
 	   return "" + phone;
    }   
    
    
    public static String generateRandomYear() {
		   Random rnd = new Random();  
	       int year = 1900 + rnd.nextInt(110); 
		   return "" + year;
	   }



    public static String generateRandomMonth() {
		  Random rnd = new Random();
		  String[] arrayOfMonth = { "January", "February", "March", "April",  
	          "May", "June", "July", "August", "September", "October",  
	          "November", "December"};
		  return arrayOfMonth[rnd.nextInt(arrayOfMonth.length)];
	   }


    public static String generateRandomDayOfBirth() {
		   Random rnd = new Random();  
		   int dayOfBirth = rnd.nextInt(31); 
		   if (dayOfBirth==0) {
			   return "-";
		   } else {
		   return "" + dayOfBirth; 
		   }
	   }

}
